package medium;
/*
59. 螺旋矩阵 II
解题思路：模拟法
 */
public class LeeCode59 {

  public static void main(String[] args) {
    int[][] res = new LeeCode59().generateMatrix(4);
    for (int[] re : res) {
      for (int i : re) {
        System.out.print(i + " ");
      }
      System.out.println();
    }
  }

  public int[][] generateMatrix(int n){
    int[][] res = new int[n][n];
    int left = 0;
    int right = n - 1;
    int up = 0;
    int down = n - 1;
    int index = 1;
    while (index <= n * n){
      for (int i = left; i <= right; i++) {
        res[up][i] = index++;
      }
      up++;
      for (int i = up; i <= down ; i++) {
        res[i][right] = index++;
      }
      right--;
      for (int i = right; i >= left; i--) {
        res[down][i] = index++;
      }
      down--;
      for(int i = down; i >= up; i--){
        res[i][left] = index++;
      }
      left++;
    }

    return res;
  }

}
